<template>
  <my-layout sTitle="购物车" :isShowLeft="false" class="cart-wrapper">
    <div slot="content" class="cart-content">
      <div class="content-box" v-if="isShowEmpty">
        <div class="cart-list-wrapper">
          <div
            v-for="(item,i) in cartList"
            :key="i"
            :class="{'list-box': true,'pitch-on-sy': item.checkbox}"
          >
            <div class="list-checkbox">
              <div class="checkbox-icon" @click="radioBtn(i)">
                <img src="@/assets/images/Oval1.png" v-show="!item.checkbox" alt>
                <img src="@/assets/images/Oval2.png" v-show="item.checkbox" alt>
              </div>
            </div>
            <div class="list-img-box">
              <img :src="item.logo" alt>
            </div>
            <div class="list-guige">
              <div class="list-title">{{item.title}}</div>
              <div class="list-gs" :class="{'list-bgw': !item.specs}">
                <span v-for="(value,key,i) in item.specs" :key="i">{{value}}</span>

                <!-- <span>{{item.specs}}</span> -->
              </div>
              <div class="list-price">
                <span>￥{{item.price*item.num}}</span>
                <!-- <span class="list-cx">￥{{item.price2}}</span> -->
                <div class="handle-num handle-sty">
                  <div class="handle-num-box handle-num-sub" @click="cartSub(i)">-</div>
                  <div class="handle-num-box handle-txt">{{item.num}}</div>
                  <div class="handle-num-box handle-num-add" @click="cartAdd(i)">+</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="cart-total">
          <div class="cart-total-left">
            <span class="choose-btn" @click="selectAll()">
              <img src="@/assets/images/Oval1.png" v-show="!isShowClass" alt>
              <img src="@/assets/images/Oval2.png" v-show="isShowClass" alt>
            </span>
            <span class="txt1">全选</span>
            <span class="txt2">共{{allPiece}}件</span>
          </div>
          <div class="cart-total-right">
            <span class="cart-total-price">
              合计：
              <b>￥{{total}}</b>
            </span>
            <span class="cart-clear" @click="showDel">删除</span>
            <span class="cart-outBtn">导出</span>
          </div>
        </div>
      </div>
      <dialog-del v-show="isShowDel" @hideDel="hideDel" @delCart="delCart"/>
      <div class="empty-box" v-if="!isShowEmpty">
        <img src="@/assets/images/emptyCart.png" alt>
        <div class="empty-txt">购物车空空如也，快去加购吧~</div>
      </div>
    </div>
    <my-footer slot="footers"/>
  </my-layout>
</template>

<script>
import DialogDel from "../../components/dialog";
export default {
  components: {
    DialogDel
  },
  data() {
    return {
      cartList: [],
      isShowClass: false,
      isShowEmpty: true,
      specs: [],
      clearList: [],
      isShowDel: false
    };
  },
  methods: {
    // 获取购物车列表
    async getCart() {
      const { data } = await this.$http.post("cart/my");
      if (data.code == 0) {
        this.cartList = data.carts.map(item => {
          item.specs = JSON.parse(item.specs);
          item.checkbox = false;
          return item;
        });
        if (this.cartList.length == 0) {
          this.isShowEmpty = false;
        } else {
          this.isShowEmpty = true;
        }
      }
    },
    // 删除购物车
    async delCart() {
      this.cartList.forEach(item => {
        if (item.checkbox) {
          this.clearList.push(item.id);
        }
      });
      const { data } = await this.$http.post("cart/dels", {
        ids: JSON.stringify(this.clearList)
      });
      if (data.code == 0) {
        this.isShowDel = false;
        this.getCart();
      }
    },
    // 显示删除弹框
    showDel() {
      let flag = false;
      this.cartList.forEach(item => {
        if (item.checkbox) {
          flag = true;
        }
      });
      if (flag) this.isShowDel = true;
    },
    // 关闭删除弹框
    hideDel() {
      this.isShowDel = false;
    },
    radioBtn(i) {
      this.cartList[i].checkbox = !this.cartList[i].checkbox;
      let flag = true;
      this.cartList.forEach(item => {
        if (!item.checkbox) {
          flag = false;
        }
      });
      this.isShowClass = flag;
    },
    selectAll() {
      this.cartList.forEach(item => {
        item.checkbox = false;
        item.checkbox = !this.isShowClass;
      });
      this.isShowClass = !this.isShowClass;
    },
    cartSub(i) {
      if (this.cartList[i].num > 1) {
        this.cartList[i].num--;
      } else {
        this.cartList[i].num = 1;
      }
    },
    cartAdd(i) {
      this.cartList[i].num++;
    },
    outBtn() {
      console.log("导出二维码");
    }
  },
  computed: {
    total() {
      let total = 0;
      this.cartList.forEach(item => {
        if (item.checkbox) {
          total += item.num * item.price;
        }
      });
      return total;
    },
    allPiece() {
      let all = 0;
      this.cartList.forEach(item => {
        if (item.checkbox) {
          all += item.num;
        }
      });
      return all;
    }
  },
  mounted() {
    this.getCart();
  }
};
</script>

<style lang="scss">
.cart-wrapper {
  position: relative;
  .content {
    background: #f7f7f7;
    position: relative;
    .content-box {
      .cart-list-wrapper {
        padding: 0 48px 300px;
        overflow: hidden;
        .list-box {
          margin-top: 24px;
          padding: 24px;
          height: 328px;
          background: #fff;
          border-radius: 10px;
          overflow: hidden;
          .list-checkbox {
            float: left;
            .checkbox-icon {
              width: 36px;
              height: 36px;
              border-radius: 50%;
              // border: 1px solid #7f7f7f;
              margin-top: 120px;
              margin-left: 20px;
            }
            .checkbox-choose {
              // border: 1px solid #ff0000;
            }
          }
          .list-img-box {
            float: left;
            margin-left: 24px;
            img {
              height: 280px;
              width: 280px;
            }
          }
          .list-guige {
            float: left;
            margin-left: 60px;
            .list-title {
              font-size: 32px;
              font-weight: 500;
              color: #333;
              margin-top: 30px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              max-width: 452px;
            }
            .list-gs {
              display: inline-block;
              height: 64px;
              background: #f9f8ff;
              border: 1px solid #f7f7f7;
              border-radius: 11px;
              line-height: 64px;
              padding: 0 40px;
              color: #333;
              font-size: 28px;
              margin-top: 30px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              max-width: 452px;
              span {
                margin-right: 20px;
              }
              span:last-child {
                margin-right: 0;
              }
            }
            .list-bgw {
              background: transparent;
              border: none;
            }
            .list-price {
              margin-top: 35px;
              position: relative;
              color: #d0021b;
              font-size: 36px;
              font-weight: 500;
              line-height: 48px;
              width: 375px;
              .list-cx {
                margin-left: 28px;
                color: #9b9b9b;
                text-decoration: line-through;
              }
              .handle-num {
                position: absolute;
                display: flex;
                font-size: 28px;
                color: #999;
                top: 0;
                right: -40px;
                .handle-num-box {
                  flex: 1;
                  width: 50px;
                  line-height: 46px;
                  height: 46px;
                  text-align: center;
                  align-items: center;
                  justify-content: center;
                }
                .handle-txt {
                  border-left: 1px solid #e5e2e2;
                  border-right: 1px solid #e5e2e2;
                  color: #333333;
                  font-size: 24px;
                }
              }
              .handle-sty {
                border: 1px solid #e5e2e2;
                margin-left: 45px;
              }
            }
          }
        }
        .pitch-on-sy {
          background: #fef9f9;
        }
      }
      .cart-total {
        height: 98px;
        line-height: 98px;
        position: fixed;
        bottom: 140px;
        display: flex;
        width: 1080px;
        background: #fff;
        .cart-total-left {
          flex: 1;
          overflow: hidden;
          height: 98px;
          span {
            float: left;
          }
          .choose-btn {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            margin-top: 28px;
            margin-left: 24px;

            img {
              vertical-align: top;
            }
          }
          .txt1 {
            margin-left: 16px;
            font-size: 28px;
            color: #333;
          }
          .txt2 {
            margin-left: 36px;
            font-size: 28px;
            color: #333;
          }
        }
        .cart-total-right {
          display: flex;
          flex: 2;
          overflow: hidden;
          position: relative;
          span {
            float: left;
          }
          .cart-total-price {
            flex: 1;
            font-size: 28px;
            color: #333;
            margin-left: 112px;
            b {
              color: #d0021b;
            }
          }
          .cart-outBtn {
            border-radius: 30px;
            background: #ee4433;
            height: 66px;
            line-height: 66px;
            width: 156px;
            font-size: 28px;
            text-align: center;
            margin: 16px 40px 0 24px;
            color: #fff;
          }
          .cart-clear {
            border-radius: 30px;
            background: #fff;
            height: 66px;
            line-height: 66px;
            width: 156px;
            font-size: 28px;
            text-align: center;
            margin-top: 16px;
            margin-left: 20px;
            color: #ee4433;
            border: 1px solid #ee4433;
          }
        }
      }
    }
    .empty-box {
      height: 100%;
      width: 100%;
      background: #fff;
      position: absolute;
      img {
        width: 410px;
        height: 410px;
        margin: 0 auto;
        display: block;
        margin-top: 380px;
        margin-bottom: 60px;
      }
      .empty-txt {
        text-align: center;
        font-size: 32px;
        color: #333333;
      }
    }
  }
}
</style>